package com.gingko.duominuo.utils
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.content.ContextCompat

/**
 * Describe：
 * @author bsx
 * CreateTime：2019/12/9
 */

object PermissionUtils {

    fun isGranted(context: Context, vararg permission: String): Boolean {
        permission.forEach {
            if (!isGranted(context, it)) {
                return false
            }
        }
        return true
    }


    fun isGranted(context: Context, permission: String): Boolean {
        return !isMarshmallow() || ContextCompat.checkSelfPermission(
            context,
            permission
        ) == PackageManager.PERMISSION_GRANTED
    }


    private fun isMarshmallow(): Boolean {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    }

    fun validatePermissions(permissions: Array<out String>) {
        if (permissions.isNullOrEmpty()) throw Exception("the permission can't be null")
    }
}